﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KpiConfigProject.Manager;

namespace KpiConfigProject.ChildForm
{
    public partial class KpiGroupConfigFrm : Form
    {
        public KpiGroupConfigFrm()
        {
            InitializeComponent();
        }

        private Dictionary<int, List<DataRow>> kpiMapTable, kpiDimMapTable;
        private Dictionary<int, DataGridViewRow> kpiInfoDic, dimInfoDic;
        private void KpiGroupConfigFrm_Load(object sender, EventArgs e)
        {
            InitAllData();
        }

        private void InitAllData()
        {
            //KpiGroupManager
            KpiGroupManager kpiGroupManager = new KpiGroupManager();
            DataTable table = kpiGroupManager.GetList();
            if (table != null)
                dgv_KPIGroup.DataSource = table.DefaultView;
            //KpiInfoManager
            KpiInfoManager kpiInfoManager = new KpiInfoManager();
            table = kpiInfoManager.GetList();
            if (table != null)
            {
                DataColumn column = table.Columns.Add("ch", typeof(bool));
                column.SetOrdinal(0);
                dgve_KpiInfo.DataSource = table.DefaultView;
                dgve_KpiInfo.Columns[0].Width = 20;
                for (int i = 1; i < dgve_KpiInfo.Columns.Count; i++)
                {
                    dgve_KpiInfo.Columns[i].ReadOnly = true;
                }
                kpiInfoDic = new Dictionary<int, DataGridViewRow>();
                foreach (DataGridViewRow row in dgve_KpiInfo.Rows)
                {
                    kpiInfoDic.Add(int.Parse(row.Cells[2].Value.ToString()), row);
                }
            }
            //DimensionManager
            DimensionManager dimManager = new DimensionManager();
            table = dimManager.GetList();
            if (table != null)
            {
                DataColumn column = table.Columns.Add("ch", typeof(bool));
                column.SetOrdinal(0);
                dgve_Dim.DataSource = table.DefaultView;
                dgve_Dim.Columns[0].Width = 20;
                for (int i = 1; i < dgve_Dim.Columns.Count; i++)
                {
                    dgve_Dim.Columns[i].ReadOnly = true;
                }
                dimInfoDic = new Dictionary<int, DataGridViewRow>();
                foreach (DataGridViewRow row in dgve_Dim.Rows)
                {
                    dimInfoDic.Add(int.Parse(row.Cells[3].Value.ToString()), row);
                }
            }
            //KpiMappingManager
            KpiMappingManager kpiMappingManager = new KpiMappingManager(); ;
            kpiMapTable = kpiMappingManager.GetGroupDictionary();
            //KpiGroupDimensionManager
            KpiGroupDimensionManager kpiGroupDimensionManager = new KpiGroupDimensionManager();
            kpiDimMapTable = kpiGroupDimensionManager.GetGroupDictionary();
        }

        private void tsb_Add_Click(object sender, EventArgs e)
        {
            dgv_KPIGroup.AddNewRow();
        }

        private void tsb_Refresh_Click(object sender, EventArgs e)
        {
            InitAllData();
        }

        private void tsb_Delete_Click(object sender, EventArgs e)
        {
            dgv_KPIGroup.DeleteRows();
        }

        private void tsb_Reduction_Click(object sender, EventArgs e)
        {
            dgv_KPIGroup.RestoreRows();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsb_Save_Click(object sender, EventArgs e)
        {
            KpiGroupManager manager = new KpiGroupManager();
            int rowIndex = dgv_KPIGroup.SelectedCells[0].RowIndex;
            DataGridViewRow selectRow = dgv_KPIGroup.Rows[rowIndex];
            int groupId = int.Parse(selectRow.Cells[0].Value.ToString());
            List<int> groupKpiId = new List<int>();
            List<int> groupDimId = new List<int>();
            dgve_KpiInfo.EndEdit();

            foreach (DataGridViewRow row in dgve_KpiInfo.Rows)
            {
                object obj = row.Cells[0].Value;
                if (obj.ToString() == "")
                    continue;
                if (bool.Parse(obj.ToString()))
                {
                    int kpiId = int.Parse(row.Cells[1].Value.ToString());
                    groupKpiId.Add(kpiId);
                }
            }
            dgve_Dim.EndEdit();
            foreach (DataGridViewRow row in dgve_Dim.Rows)
            {
                object obj = row.Cells[0].Value;
                if (obj.ToString() == "")
                    continue;
                if (bool.Parse(obj.ToString()))
                {
                    int dimId = int.Parse(row.Cells[1].Value.ToString());
                    groupDimId.Add(dimId);
                }
            }
            KpiMappingManager kpiMappingManager = new KpiMappingManager();
            bool isSucc = kpiMappingManager.InsertData(groupId,groupKpiId);
            KpiGroupDimensionManager groupDimensionManager = new KpiGroupDimensionManager();
            isSucc = groupDimensionManager.InsertData(groupId, groupDimId);
            if (isSucc)
            {
                //dgv_KPIGroup.ClearRowData();
                MessageBox.Show("保存数据成功！");
            }
        }

        private void dgv_KPIGroup_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex <= -1)
                return;
            dgve_KpiInfo.RejectChanges();
            dgve_Dim.RejectChanges();
            DataGridViewRow SelectRow = dgv_KPIGroup.Rows[e.RowIndex];
            int groupId = int.Parse(SelectRow.Cells[0].Value.ToString());
            if (kpiMapTable.ContainsKey(groupId))
            {
                List<DataRow> kpiRows = kpiMapTable[groupId];
                foreach (DataRow row in kpiRows)
                {
                    int kpiId = int.Parse(row["intKpiInfoId"].ToString());
                    if (kpiInfoDic.ContainsKey(kpiId))
                    {                        
                        kpiInfoDic[kpiId].Cells[0].Value = true;                        
                    }
                }
            }

            if (kpiDimMapTable.ContainsKey(groupId))
            {
                List<DataRow> kpiRows = kpiDimMapTable[groupId];
                foreach (DataRow row in kpiRows)
                {
                    int dimId = int.Parse(row["intDimensionId"].ToString());
                    if (kpiInfoDic.ContainsKey(dimId))
                    {
                        kpiInfoDic[dimId].Cells[0].Value = true;
                    }
                }
            }
        }
    }
}
